package com.wbao.dianniu.customView;

import android.content.Context;
import android.support.annotation.ColorInt;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.util.AttributeSet;


import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by yejianjun on 2018/5/24.
 */

public class SpanEditText extends android.support.v7.widget.AppCompatEditText {

    // 定义正则表达式
    private static final String AT = "@[\u4e00-\u9fa5\\w]+";// @人
    private static final String EMOJI = "\\[[\u4e00-\u9fa5\\w]+\\]";// 表情
    private static final String COMMENT = "回复[\u4e00-\u9fa5\\w]+:";
    private static final String TOPIC = "#[\u4e00-\u9fa5\\w]+#";// ##话题
    private static final String URL = "http://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]";// url
    private static final String REGEX = "(@[\u4e00-\u9fa5\\w]+)|(\\[[\u4e00-\u9fa5\\w]+\\])|(回复[\u4e00-\u9fa5\\w]+:)";

    public SpanEditText(Context context) {
        super(context);
    }

    public SpanEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public SpanEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void setSpanTextColor(int start, @ColorInt int color) {

    }

    public void setSpanTextColor(String str, @ColorInt int color) {

        int[] size = getStartAndEnd(str);
        setSpanTextColor(size[0], size[1], color);
    }

    public void setASpanText(String str,@ColorInt int color){
        //设置正则
        Pattern pattern = Pattern.compile(AT);
        Matcher matcher = pattern.matcher(str);
        while (matcher.find()){
            String at = matcher.group();
            if (at != null) {
                //获取匹配位置
                int start = matcher.start();
                int end = start + at.length();
                String name = at.substring(1,at.length());
                SpannableString spannableString = new SpannableString(str);
                spannableString.setSpan(null, start+1, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        }

    }


    /**
     * 设置文字颜色
     *
     * @param start 开始的位置
     * @param end   结束的位置
     * @param color 要设置的颜色
     */
    public void setSpanTextColor(int start, int end, @ColorInt int color) {
        setSpann(createSpan(getText(), new ForegroundColorSpan(color), start, end));
    }

    private int[] getStartAndEnd(String text) {
        int start = getText().toString().indexOf(text);
        return new int[]{start, text.length() + start};
    }

    /**
     * 设置文字颜色
     *
     * @param start    开始的位置
     * @param end      结束的位置
     * @param textSize 要设置的大小，单位px，请自行根据sp转换
     */
    public void setSpanTextSize(int start, int end, int textSize) {
        setSpann(createSpan(getText(), new AbsoluteSizeSpan(textSize), start, end));
    }

    public SpannableString createSpan(CharSequence text, Object spann, int start, int end) {

        if (start < 0 || end <= 0 || spann == null) {
            return null;
        }

        SpannableString spannableString = new SpannableString(text);
        spannableString.setSpan(spann, start, end, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        return spannableString;
    }

    public void setSpann(SpannableString spannableString) {
        if (spannableString == null) {
            return;
        }
        setText(spannableString);
    }

}
